﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Northwind.Management.Employee;
using System.IO;
using Northwind.Objects;
using Db4objects.Db4o;
using Northwind.Management;
using Northwind.Management.Customer;
using Northwind.Management.Product;

namespace Northwind
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        readonly static string DBFileName = GetAppFullPath("Northwind.db4o");

        private static string GetAppFullPath(string pFileName)
        {
            return System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), pFileName);
        }

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string path = "pack://application:,,,/Northwind;component/Resources/Images/Background.jpg";
            ImageBrush myBrush = new ImageBrush();
            Uri uriSource = new Uri(path, UriKind.Absolute);
            myBrush.ImageSource = new BitmapImage(uriSource);
            Label_Background.Background = myBrush;
        }


        private void RibbonMenuItem_Customer_View_Click(object sender, RoutedEventArgs e)
        {
            UserControl_Customer UC_Customer = new UserControl_Customer();
            UIPanel.Children.Clear();
            UIPanel.Children.Add(UC_Customer);
        }

        private void RibbonButton_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void RibbonMenuItem_Order_New_Click_1(object sender, RoutedEventArgs e)
        {
            UserControl_Order UC_Order = new UserControl_Order();
            UC_Order.SetParamaters(OrderViewModeEnum.New);
            UIPanel.Children.Clear();
            UIPanel.Children.Add(UC_Order);
        }

        private void DoImport()
        {
            try
            {
                using (IObjectContainer db = Db4oEmbedded.OpenFile(DBFileName))
                {
                    // Import Customer.
                    using (StreamReader DataFile = new StreamReader(GetAppFullPath("Customers.txt")))
                    {
                        while (!DataFile.EndOfStream)
                        {
                            Customer customer = new Customer(DataFile.ReadLine());
                            db.Store(customer);
                        }
                    }
                    IList<Customer> customers = db.Query<Customer>();
                    // Import Product.
                    using (StreamReader DataFile = new StreamReader(GetAppFullPath("Products.txt")))
                    {
                        while (!DataFile.EndOfStream)
                        {
                            Product product = new Product(DataFile.ReadLine());
                            db.Store(product);
                        }
                    }
                    IList<Product> products = db.Query<Product>();
                    // Import Employees.
                    using (StreamReader DataFile = new StreamReader(GetAppFullPath("Employees.txt")))
                    {
                        while (!DataFile.EndOfStream)
                        {
                            Employee employee = new Employee(DataFile.ReadLine());
                            db.Store(employee);
                        }
                    }
                    IList<Employee> employees = db.Query<Employee>();
                    MessageBox.Show(string.Format("{0} Customers. {1} Products. {2} Employees.", customers.Count, products.Count, employees.Count), "Results", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    MessageBox.Show(string.Format("Import states failed. \r\n{0}", ex.InnerException.Message), "Data", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ClearData()
        {
            try
            {
                System.IO.File.Delete(DBFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Import states failed. \r\n{0}", ex.InnerException.Message), "Data", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void RibbonMenuItem_Product_View_Click(object sender, RoutedEventArgs e)
        {
            UserControl_Product UC_Order = new UserControl_Product();
            UIPanel.Children.Clear();
            UIPanel.Children.Add(UC_Order);
        }

        private void RibbonButton_Import_Click_1(object sender, RoutedEventArgs e)
        {
            DoImport();
        }

        private void RibbonButton_Clear_Click_1(object sender, RoutedEventArgs e)
        {
            ClearData();
        }

        private void RibbonApplicationMenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to quit ?", "Northwind", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (result == MessageBoxResult.Yes)
            {
                this.Close();
            }
        }

        private void RibbonButton_Click_2(object sender, RoutedEventArgs e)
        {
            WindowAbout windowAbout = new WindowAbout();
            windowAbout.ShowDialog();
        }

        private void RibbonMenuItem_Order_Find_Click_1(object sender, RoutedEventArgs e)
        {
            UserControl_FindOrder UC_FindOrder = new UserControl_FindOrder();
            UIPanel.Children.Clear();
            UIPanel.Children.Add(UC_FindOrder);
        }
    }
}
